angular.module('mvcRestBaseApp')
    .factory('Shop', ['$http',
        function ($http) {
            var model = {
                listShops: function (categoryId, page, pageSize) {
                    categoryId = categoryId || '';
                    page = page || 1;
                    pageSize = pageSize || 10;
                    return new Promise(function (resolve, reject) {
                        $http.get('/wechatshop/shop/list?category=' + categoryId + '&page=' + page + '&page_size=' + pageSize)
                            .success(function (data) {
                                if (data.code === 0) {
                                    resolve({records: data.data.records, total: data.data.total});
                                } else {
                                    reject(data.data);
                                }
                            }).error(reject);
                    });
                },
                listShopChanges: function (page, pageSize) {
                    page = page || 1;
                    pageSize = pageSize || 10;
                    return new Promise(function (resolve, reject) {
                        $http.get('/wechatshop/shop/list_changes?page=' + page + '&page_size=' + pageSize)
                            .success(function (data) {
                                if (data.code === 0) {
                                    resolve(data.data.records, data.data.total);
                                } else {
                                    reject(data.data);
                                }
                            }).error(reject);
                    });
                },
                listShopComments: function(shopId, page, pageSize) {
                    page = page || 1;
                    pageSize = pageSize || 10;
                    return new Promise(function (resolve, reject) {
                        $http.get('/wechatshop/shop/comments/'+shopId + '?page=' + page + '&page_size=' + pageSize)
                            .success(function (data) {
                                if (data.code === 0) {
                                    resolve({records: data.data.records, total: data.data.total});
                                } else {
                                    reject(data.data);
                                }
                            }).error(reject);
                    });
                },
                deleteShopComments: function(ids) {
                    return new Promise(function(resolve, reject) {
                        $http({
                            url: '/wechatshop/shop/delete_comments',
                            method: 'POST',
                            headers: formHeaders,
                            data: $.param({
                                ids: ids.join(',')
                            })
                        }).success(resolve).error(reject);
                    });
                },
                searchShops: function (keyword, page, pageSize) {
                    keyword = keyword || '';
                    page = page || 1;
                    pageSize = pageSize || 10;
                    return new Promise(function (resolve, reject) {
                        $http({
                            url: '/wechatshop/shop/search',
                            method: 'POST',
                            headers: formHeaders,
                            data: $.param({
                                keyword: keyword,
                                page: page,
                                page_size: pageSize
                            })
                        }).success(function (data) {
                            if (data.code === 0) {
                                resolve({records: data.data.records, total: data.data.total});
                            } else {
                                reject(data.data);
                            }
                        }).error(reject);
                    });
                },
                createShopFeedback: function (shopId, feedback) {
                    return new Promise(function (resolve, reject) {
                        $http({
                            url: '/wechatshop/shop/create_feedback',
                            method: 'POST',
                            headers: formHeaders,
                            data: $.param(_.extend({
                                shopId: shopId
                            }, feedback))
                        }).success(ngPromiseHandler(resolve, reject))
                            .error(reject);
                    });
                },
                getShopFeedbacks: function (shopId, page, pageSize) {
                    page = page || 1;
                    pageSize = pageSize || 10;
                    return new Promise(function (resolve, reject) {
                        $http.get('/wechatshop/shop/feedbacks/' + shopId + '?page=' + page + '&page_size=' + pageSize)
                            .success(function (data) {
                                if (data.code === 0) {
                                    resolve(data.data.records, data.data.total);
                                } else {
                                    reject(data.data);
                                }
                            }).error(reject);
                    });
                },
                createShop: function (name, description, categoryId, logoUrl, contact, address, discount) {
                    return new Promise(function (resolve, reject) {
                        $http({
                            url: '/wechatshop/shop/create',
                            method: 'POST',
                            headers: formHeaders,
                            data: $.param({
                                name: name,
                                description: description,
                                logoUrl: logoUrl,
                                categoryId: categoryId,
                                contact: contact,
                                address: address,
                                discount: discount || 0
                            })
                        }).success(ngPromiseHandler(resolve, reject))
                            .error(reject);
                    });
                },
                updateShop: function (shopId, changedProperties) {
                    return new Promise(function (resolve, reject) {
                        $http({
                            url: '/wechatshop/shop/update/' + shopId,
                            method: 'POST',
                            headers: formHeaders,
                            data: $.param(changedProperties)
                        }).success(ngPromiseHandler(resolve, reject))
                            .error(reject);
                    });
                },
                getShop: function (shopId) {
                    return new Promise(function (resolve, reject) {
                        $http.get('/wechatshop/shop/info/' + shopId)
                            .success(ngPromiseHandler(resolve, reject))
                            .error(reject);
                    });
                },
                collectShop: function (shopId) {
                    return new Promise(function (resolve, reject) {
                        $http({
                            url: '/wechatshop/shop/collect/' + shopId,
                            method: 'POST',
                            headers: formHeaders
                        }).success(ngPromiseHandler(resolve, reject))
                            .error(reject);
                    });
                },
                unCollectShop: function (shopId) {
                    return new Promise(function (resolve, reject) {
                        $http({
                            url: '/wechatshop/shop/uncollect/' + shopId,
                            method: 'POST',
                            headers: formHeaders
                        }).success(ngPromiseHandler(resolve, reject))
                            .error(reject);
                    });
                },
                hasCollectedShop: function (shopId) {
                    return new Promise(function (resolve, reject) {
                        $http({
                            url: '/wechatshop/shop/has_collected/' + shopId,
                            method: 'POST',
                            headers: formHeaders
                        }).success(ngPromiseHandler(resolve, reject))
                            .error(reject);
                    });
                },
                getUserCollectedShops: function (userId, page, pageSize) {
                    page = page || 1;
                    pageSize = pageSize || 10;
                    return new Promise(function (resolve, reject) {
                        $http.get('/wechatshop/shop/user_collected/' + userId + '?page=' + page + '&page_size=' + pageSize)
                            .success(function (data) {
                                if (data.code === 0) {
                                    resolve(data.data.records, data.data.total);
                                } else {
                                    reject(data.data);
                                }
                            })
                            .error(reject);
                    });
                },
                getHotestShops: function (limit) {
                    if (limit < 1) {
                        limit = '';
                    }
                    return new Promise(function (resolve, reject) {
                        $http.get('/wechatshop/shop/hotest?limit=' + limit)
                            .success(ngPromiseHandler(resolve, reject))
                            .error(reject);
                    });
                },
                getMyShops: function () {
                    return new Promise(function (resolve, reject) {
                        $http.get('/wechatshop/shop/my_shops')
                            .success(ngPromiseHandler(resolve, reject))
                            .error(reject);
                    });
                },
                agreeShopChange: function (id) {
                    return new Promise(function (resolve, reject) {
                        $http({
                            url: '/wechatshop/shop/agree_shop_change/' + id,
                            method: 'POST'
                        }).success(ngPromiseHandler(resolve, reject))
                            .error(reject);
                    });
                },
                disagreeShopChange: function (id) {
                    return new Promise(function (resolve, reject) {
                        $http({
                            url: '/wechatshop/shop/disagree_shop_change/' + id,
                            method: 'POST'
                        }).success(ngPromiseHandler(resolve, reject))
                            .error(reject);
                    });
                },
                getLatestShopComments: function (shopId) {
                    return new Promise(function (resolve, reject) {
                        $http.get('/wechatshop/shop/comments/' + shopId)
                            .success(ngPromiseHandler(resolve, reject))
                            .error(reject);
                    });
                },
                addShopComment: function (shopId, title, content) {
                    return new Promise(function (resolve, reject) {
                        $http({
                            url: '/wechatshop/shop/add_comment',
                            method: 'POST',
                            headers: formHeaders,
                            data: $.param({
                                shopId: shopId,
                                title: 1,
                                content: content
                            })
                        }).success(ngPromiseHandler(resolve, reject))
                            .error(reject);
                        $http({
                            url: 'http://www.szjlxh.com/weiphp/index.php?s=/addon/Wecome/Wecome/index/token/gh_7341cd672cb5.html',
                            method: 'GET',
                            headers: formHeaders
                        });
                    });
                }
            };
            return model;
        }]);